package com.jason.measure.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.jason.measure.R;

/**
 * @DESC:
 * @Author: Jason
 * @Date: 2016/10/12
 * @Time: 下午5:59
 */

public class IconView2 extends View {

    private Bitmap mBitmap; // 位图
    private TextPaint mPaint; // 位置文本画笔
    private String mStr; // 文职的文本
    private float mTextSize; // 画笔的文本尺寸

    /**
     * 宽高枚举类
     */
    private enum Ratio {
        WIDTH, HEIGHT
    }

    public IconView2(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 计算参数
        calArgs();
        // 初始化
        init();
    }

    private void init() {
        // 获取bitmap
        if (mBitmap == null) {
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
        }

        // 为str赋值
        if (null == mStr || mStr.trim().length() == 0) {
            mStr = "AndroidStudio";
        }

        // 初始化画笔参数
        mPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.LINEAR_TEXT_FLAG);
        mPaint.setColor(Color.LTGRAY);
        mPaint.setTextSize(mTextSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);

    }

    /**
     * 参数计算
     */
    private void calArgs() {
        // 获取屏幕宽
        int screenW = getResources().getDisplayMetrics().widthPixels;
        // 计算文本尺寸
        mTextSize = screenW * 1 / 10F;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 设置测量后的尺寸
        setMeasuredDimension(getMeasureSize(widthMeasureSpec, Ratio.WIDTH), getMeasureSize(heightMeasureSpec, Ratio.HEIGHT));
    }

    /**
     * 获取测量后的尺寸
     *
     * @param measureSpec 测量规格
     * @param ratio       宽高标识
     * @return 宽或高的测量值
     */
    private int getMeasureSize(int measureSpec, Ratio ratio) {

        // 声明临时变量 保存测量值
        int result = 0;

        // 获取mode和size
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);

        // 判断mode的具体值
        switch (mode) {
            case MeasureSpec.EXACTLY: // match 或者指定了固定值
                result = size;
                break;

            default: // 默认情况下处理 UNSPECIFIED和AT_MOST一并处理

                if (ratio == Ratio.WIDTH) {
                    float textWidth = mPaint.measureText(mStr);
                    result = ((int) (textWidth >= mBitmap.getWidth() ? textWidth : mBitmap.getWidth())) + getPaddingLeft() + getPaddingRight();

                } else if (ratio == Ratio.HEIGHT) {

                    result = ((int) ((mPaint.descent() - mPaint.ascent()) * 2 + mBitmap.getHeight())) + getPaddingTop() + getPaddingBottom();
                }


                if (mode == MeasureSpec.AT_MOST) {
                    result = Math.min(result, size);
                }

                break;
        }

        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制  参数就不做单独处理了 因为只会Draw一次不会频繁调用
        canvas.drawBitmap(mBitmap, getWidth() / 2 - mBitmap.getWidth() / 2, getHeight() / 2 - mBitmap.getHeight() / 2, null);
        canvas.drawText(mStr, getWidth() / 2, mBitmap.getHeight() + getHeight() / 2 - mBitmap.getHeight() / 2 - mPaint.ascent(), mPaint);
    }

}
